/**
 * 获取短信验证码 mixins
 */

import { isMoblie } from '@/utils/validate'
export default {
  data() {
    return {
      smsAnewTime: 0, // 短信重发时间、秒
      smsTimer: null
    }
  },

  beforeDestroy() {
    if (this.smsTimer) clearInterval(this.smsTimer)
  },

  methods: {
    /**
     * 获取短信验证码
     * @param {*} mobile 手机号码
     * @param {*} url 请求地址
     */
    getSMSCode(mobile, url) {
      // const mobile = this.dataForm.mobile
      if (mobile === '') {
        return this.$message.warning('请输入手机号')
      } else if (!isMoblie(mobile)) {
        return this.$message.warning('请输入正确格式的手机号码')
      }

      this.smsAnewTime = 60
      this.smsTimer = setInterval(() => {
        this.smsAnewTime--
        if (this.smsAnewTime <= 0) {
          clearInterval(this.smsTimer)
        }
      }, 1000)
      url = url || '/user/sendVerificationCodeByMobile'
      this.$http({
        method: 'post',
        url,
        params: { mobile }
      }).catch(() => {
        this.smsAnewTime = 0
        clearInterval(this.smsTimer)
      })
    }
  }
}
